﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace jQuery.Ajax
{
    public static class RemainTime
    {
        public static string TargetTime
        {
            get { return ConfigurationManager.AppSettings["ActivityTime"]; }
        }

        public static string ReturnJson()
        {
            var jsonResult = string.Empty;

            if (!string.IsNullOrEmpty(TargetTime))
            {
                var t1 = new TimeSpan(Convert.ToDateTime(TargetTime).Ticks);
                var t2 = new TimeSpan(DateTime.Now.Ticks);
                var ts = t1.Subtract(t2).Duration();
                var day = ts.Days.ToString();
                var hour = ts.Hours.ToString();
                var minute = ts.Minutes.ToString();
                var second = ts.Seconds.ToString();

                jsonResult = string.Format(
                        "{{\"beginTime\":\"{0}\",\"nowTime\":\"{5}\",\"remainTime\":{{\"d\":\"{1}\",\"h\":\"{2}\",\"m\":\"{3}\",\"s\":\"{4}\"}}}}",
                        TargetTime, day, hour, minute, second, DateTime.Now);
            }

            return jsonResult;
        }
    }
}